<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="广告图名称" prop="advimgdesc">
        <el-input v-model="formData.advimgdesc" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="广告位" prop="advimgpos">
        <!-- 下拉框 -->
        <el-select v-model="formData.advimgpos">
          <el-option
            v-for="item in allPostList"
            :key="item.id"
            :label="item.advposname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告链接" prop="advimglink">
        <el-input v-model="formData.advimglink" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="广告图" prop="advimgsrc">
        <!-- 自己上传 action  http-request -->
        <!-- fileList 作用就是回显！ -->
        <el-upload
          action="#"
          :file-list="fileList"
          :http-request="httpRequest"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addAdvImgInfoApi, updateAdvImgInfoApi } from '@/api/adv_img'
import { getAdvPosAllListApi } from '@/api/adv_pos'
import { UploadFile } from '@/api/user'
export default {
  name: 'AddFormImg',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        advimgdesc: '',
        advimglink: '',
        advimgpos: '',
        advimgsrc: ''
      },
      // 表单验证规则
      formRules: {
        advimgdesc: [{ required: true, message: '请输入', trigger: 'blur' }],
        advimglink: [{ required: true, message: '请输入', trigger: 'blur' }],
        advimgpos: [{ required: true, message: '请输入', trigger: 'blur' }],
        advimgsrc: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      allPostList: [], // 所有的广告位
      fileList: [] // 回显 {url,name}
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    // 调用初始化
    this.initData()
  },
  methods: {
    // 初始化数据
    async initData() {
      // 获取所有的广告位
      const res2 = await getAdvPosAllListApi()
      this.allPostList = res2
    },
    // 提交行为
    async onSubmit() {
      // 提交的时候从fileList里面取出来给advimgsrc
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateAdvImgInfoApi(this.formData)
        : await addAdvImgInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        advposdesc: '',
        advposname: '',
        advpossize: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
      // 清除
      this.fileList = []
    },
    // 上传行为
    async httpRequest(result) {
      // 上传
      const fd = new FormData()
      fd.append('file', result.file)
      const res = await UploadFile(fd)
      // 服务器端存储地址放在表单字段里面
      // 思路1：放在表单里面
      this.formData.advimgsrc = res.savePath
      // 思路2：放在fileList里面去
    }
  }
}
</script>
<style scoped></style>
